package Dao;

import Bean.OrderGoods;
import Utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class orderGoodsDao {

    public static List<OrderGoods> getOrderGoods(){
        List<OrderGoods> list = new ArrayList<>();
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "select * from tb_ordergoods";
            //执行sql
            pst = con.prepareStatement(sql);
            //接收结果
            rs = pst.executeQuery();
            while (rs.next()){
                OrderGoods orderGoods = new OrderGoods();
                orderGoods.setId(rs.getInt(1));
                orderGoods.setCust(rs.getString(2));
                orderGoods.setOid(rs.getInt(3));
                orderGoods.setDeDate(rs.getString(4));
                orderGoods.setGoodsName(rs.getString(5));
                orderGoods.setPrice(rs.getFloat(6));
                orderGoods.setNum(rs.getInt(7));
                list.add(orderGoods);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return  list;
    }

    public static List<OrderGoods> query(String o,String v){
        List<OrderGoods> list = new ArrayList<>();
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String sql = "";
        if("客户".equals(o)){
            sql =  "select * from tb_ordergoods where cust like ?  ";
        }else if("订单号".equals(o)){
            sql = "select * from tb_ordergoods where oid like ?  ";
        }else {
            sql = "select * from tb_ordergoods where deDate like ?  ";
        }

        try {
            con = JDBCUtils.getConnection();

            //执行sql
            pst = con.prepareStatement(sql);
            //设置参数
//            pst.setString(1,o);
            pst.setString(1,"%"+v+"%");
            System.out.println(sql);
            //接收结果
            rs = pst.executeQuery();

            while (rs.next()){
                OrderGoods orderGoods = new OrderGoods();
                orderGoods.setId(rs.getInt(1));
                orderGoods.setCust(rs.getString(2));
                orderGoods.setOid(rs.getInt(3));
                orderGoods.setDeDate(rs.getString(4));
                orderGoods.setGoodsName(rs.getString(5));
                orderGoods.setPrice(rs.getFloat(6));
                orderGoods.setNum(rs.getInt(7));
                list.add(orderGoods);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        System.out.println(list);
        return  list;
    }

    public static  Boolean addOrderGoods(OrderGoods goods){
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            con = JDBCUtils.getConnection();
            String sql = "insert into tb_ordergoods value(?,?,?,?,?,?,?);";
            //执行sql
            pst = con.prepareStatement(sql);
            //设置参数
            pst.setInt(1,0);
            pst.setString(2,goods.getCust());
            pst.setInt(3,goods.getOid());
            pst.setString(4,goods.getDeDate());
            pst.setString(5,goods.getGoodsName());
            pst.setFloat(6,goods.getPrice());
            pst.setInt(7,goods.getNum());
            //接收结果
            int i = pst.executeUpdate();
            if(i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return  false;
    }

    public static boolean modify(OrderGoods goods){
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            con = JDBCUtils.getConnection();
            String sql = "update tb_ordergoods set oid=?,cust=?,deDate=?,goodsName=?,price=?,num=? where oid = ?";
            //执行sql
            pst = con.prepareStatement(sql);
            //设置参数
            pst.setInt(1,goods.getOid());
            pst.setString(2,goods.getCust());
            pst.setString(3,goods.getDeDate());
            pst.setString(4,goods.getGoodsName());
            pst.setFloat(5,goods.getPrice());
            pst.setInt(6,goods.getNum());
            pst.setInt(7,goods.getOid());
            //接收结果
            int i = pst.executeUpdate();
            if(i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return  false;
    }

    public static boolean delete (int oid){
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            con = JDBCUtils.getConnection();
            String sql = "delete from tb_ordergoods  where oid = ?";
            //执行sql
            pst = con.prepareStatement(sql);
            //设置参数
            pst.setInt(1,oid);
            //接收结果
            int i = pst.executeUpdate();
            if(i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return false;
    }
}
